package org.dromara.kitchen.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.dromara.kitchen.domain.kitchen.KitchenFoodType;
import org.dromara.kitchen.service.IKitchenFoodTypeService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 套餐主题
 *
 * @author Mr.Zhou
 * @date 2024-12-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/kitchen/foodType")
public class KitchenFoodTypeController extends BaseController {

    private final IKitchenFoodTypeService kitchenFoodTypeService;

    /**
     * 查询套餐主题列表
     */
    @SaCheckPermission("kitchen:foodType:list")
    @GetMapping("/list")
    public TableDataInfo<KitchenFoodType> list(KitchenFoodType kitchenFoodType, PageQuery pageQuery) {
        QueryWrapper<KitchenFoodType> qw = new QueryWrapper<>();

        TableDataInfo<KitchenFoodType> td = PageUtils.buildDataInfo(kitchenFoodTypeService.page(PageUtils.buildPage(),qw));
        List<KitchenFoodType> list = td.getRows();
        return td;
    }


    /**
     * 查询套餐主题列表  不分页
     */
    @GetMapping("/noPage/list")
    public R<List<KitchenFoodType>> noPageList(KitchenFoodType foodType) {
        QueryWrapper<KitchenFoodType> qw = new QueryWrapper<>();
        List<KitchenFoodType> list = kitchenFoodTypeService.list(qw);
        return R.ok(list);
    }



    /**
     * 导出套餐主题列表
     */
    @SaCheckPermission("kitchen:foodType:export")
    @Log(title = "套餐主题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(KitchenFoodType kitchenFoodType, HttpServletResponse response) {
        List<KitchenFoodType> list = kitchenFoodTypeService.list();
        ExcelUtil.exportExcel(list, "套餐主题", KitchenFoodType.class, response);
    }

    /**
     * 获取套餐主题详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("kitchen:foodType:query")
    @GetMapping("/{id}")
    public R<KitchenFoodType> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer id) {
        return R.ok(kitchenFoodTypeService.getById(id));
    }

    /**
     * 新增套餐主题
     */
    @SaCheckPermission("kitchen:foodType:add")
    @Log(title = "套餐主题", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody KitchenFoodType kitchenFoodType) {
        return toAjax(kitchenFoodTypeService.save(kitchenFoodType));
    }

    /**
     * 修改套餐主题
     */
    @SaCheckPermission("kitchen:foodType:edit")
    @Log(title = "套餐主题", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody KitchenFoodType kitchenFoodType) {
        return toAjax(kitchenFoodTypeService.updateById(kitchenFoodType));
    }

    /**
     * 删除套餐主题
     *
     * @param ids 主键串
     */
    @SaCheckPermission("kitchen:foodType:remove")
    @Log(title = "套餐主题", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Integer[] ids) {
        return toAjax(kitchenFoodTypeService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
